package cn.study.luckyapp.user.command;

import cn.study.luckyapp.assembler.UserAssembler;
import cn.study.luckyclient.dto.UserRegisterCmd;
import cn.study.luckyclient.dto.data.UserVo;
import cn.study.luckydomain.User.UserEntity;
import cn.study.luckydomain.gateway.UserGateway;
import com.alibaba.cola.exception.SysException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Slf4j
@Component
@AllArgsConstructor
public class UserRegisterCmdExe {
    private final UserGateway userGateway;
    public UserVo execute(UserRegisterCmd cmd){
       UserEntity user=userGateway.findByUserName(null,cmd.getUserName());
       if(Objects.nonNull(user)){
           throw new SysException("账号存在");
       }
        UserEntity userEntity = UserAssembler.toAddEntity(cmd);
        UserEntity save = userGateway.save(userEntity);
        UserVo userVo = UserAssembler.toUserVo(save);
        return userVo;
    }
}
